#coding=gbk

import urllib, BeautifulSoup

header = "#请不要在下面边添加任何条目，否则将在下次更新时失效！\n"

web=urllib.urlopen("http://www.360kb.com/kb/2_122.html")
soup=BeautifulSoup.BeautifulSoup(web.read())
new=soup.find(id="storybox")("p")[-1]
new_content=header+unicode(str(new),"utf8").replace("&nbsp;",' ').replace("<br />","")[5:-4]
#.text.replace("&nbsp;",'').replace("<>")

hosts=open("C:\Windows\System32\drivers\etc\hosts",'r')
old_content=hosts.read()
hosts.close()

hosts=open("C:\Windows\System32\drivers\etc\hosts",'w')
if old_content.find(header)<0:
    if old_content[-1]!='\n\n\n':
        content=old_content+'\n\n\n'+new_content
    else:
         content=old_content+new_content

else:
    old_content=old_content[:old_content.find(header)]
    content=old_content+new_content
    
hosts.write(content)
hosts.close()